<template>
  <v-container app>
    <v-container>
    <v-form
      ref="form"
    >
      <v-text-field
        v-model="username"
        :counter="50"
        label="姓名"
        :rules="nameRules"
        required
      ></v-text-field>

      <v-text-field
        v-model="loginname"
        :counter="50"
        label="登录名"
        required
        placeholder=" "
      ></v-text-field>

      <v-text-field
        v-model="pwd"
        label="密码"
        type="password"
        required
        placeholder=" "
      ></v-text-field>

      <v-btn
        color="success"
        class="mr-4"
        @click="save"
      >
        保存
      </v-btn>

      <v-btn
        color="error"
        class="mr-4"
        @click="back"
      >
        返回
      </v-btn>

    </v-form>
    </v-container>

    <myAlert :dialog="dialog" :msg="msg" @setParam="setParam"></myAlert>

  </v-container>
</template>

<script>
import myAlert from '@/components/common/myAlert'

export default {
  data: () => ({
    username: '',
    loginname: '',
    pwd: '',
    dialog: false,
    msg: '',
    nameRules: [
      v => !!v || '名称不能为空',
      v => (v && v.length > 1) || '名称至少2位'
    ]
  }),
  components: {
    myAlert
  },
  methods: {
    save () {
      //  this.$refs.form.validate()
      this.axios.post('user/addUser', 'username=' + this.username + '&loginname=' + this.loginname + '&pwd=' + this.pwd).then(response => {
        if (response.data.nResult === '0') {
          this.$router.replace('userList')
        } else {
          this.dialog = true
          this.msg = response.data.msg
        }
      })
    },
    back () {
      // this.$refs.form.reset()
      this.$router.replace('userList')
    },
    setParam: function (dialog) {
      this.dialog = dialog
    }
  }
}
</script>
